package com.ferris.ioc;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

@SuppressLint("UseSparseArrays")
public class OnEventListener implements OnClickListener{
	private Activity activity;
	private static Map<Integer, String> listeners = new HashMap<Integer, String>();
	public OnEventListener(Activity activity) {
		// TODO Auto-generated constructor stub
		this.activity=activity;
	}

	public OnEventListener() {

	}
	
	public OnEventListener setmClick(Integer id,String name) {
		// TODO Auto-generated method stub
		listeners.put(id, name);
		return this;
	}

	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(listeners==null||listeners.size()<=0){
			return;
		}
		
		String methodName=listeners.get(v.getId());
		if(TextUtils.isEmpty(methodName)){
			return;
		}
		  try {
			   //方法名，参数类型
	            Method callbackMethod = activity.getClass().getDeclaredMethod(methodName, View.class);
	            callbackMethod.setAccessible(true);
	            //类名 +参数
	            callbackMethod.invoke(activity, v);
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	}

	
}
